package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.BonusRecord;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.qo.BonusRecordQueryObject;
import cn.wolfcode.crm.qo.JsonResult;
import cn.wolfcode.crm.service.IBonusRecordService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("bonusRecord")
@Controller
public class BonusRecordController {

	@Autowired
	private IBonusRecordService bonusRecordService;

	@Autowired
	private ISystemDictionaryItemService systemDictionaryItemService;

	@Autowired
	private IEmployeeService employeeService;
	
	@RequestMapping("list")
	@RequiresPermissions(value = {"bonusRecord:list","部门列表"},logical = Logical.OR)
	public String list(Model model, @ModelAttribute("qo")BonusRecordQueryObject qo){
		//int i = 1/0;
		PageInfo<BonusRecord> pageInfo = bonusRecordService.query(qo);
		model.addAttribute("pageInfo",pageInfo);

		//奖金类型
		List<SystemDictionaryItem> bonusTypes = systemDictionaryItemService.selectBySystemDictionarySn("bonus");
		model.addAttribute("bonusTypes", bonusTypes);

		//员工
		List<Employee> employees = employeeService.list();
		model.addAttribute("employees", employees);
		return "bonusRecord/list";
	}
	
	/*@RequestMapping("input")
	@RequriedPermission(name="部门编辑",expression = "bonusRecord:input")
	public String input(Long id,Model model){
		if (id!=null) {
			BonusRecord bonusRecord = bonusRecordService.select(id);
			model.addAttribute("bonusRecord", bonusRecord);
		}
		return "bonusRecord/input";
	}*/
	
	/*@RequestMapping("delete")
	@RequiresPermissions(value = {"bonusRecord:delete","部门删除"},logical = Logical.OR)
	@ResponseBody
	public JsonResult delete(Long id){
		try {
			if (id!=null) {
				bonusRecordService.delete(id);
			}
			return new JsonResult(true,"删除成功");
		}catch (Exception e){
			e.printStackTrace();
			return new JsonResult(false,"删除失败");
		}
	}*/
	@RequestMapping("save")
	@ResponseBody
	public JsonResult save(BonusRecord bonusRecord){

				bonusRecordService.insert(bonusRecord);

			return new JsonResult(true,"新增或修改成功");


	}
}
